package by.msokoloff.javaweb2015.webapp.page.airport;

import java.util.Iterator;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.resource.ContextRelativeResource;
import org.apache.wicket.request.resource.IResource;

import by.msokoloff.javaweb2015.datamodel.Airport;
import by.msokoloff.javaweb2015.datamodel.Airport_;
import by.msokoloff.javaweb2015.services.AirportService;
import by.msokoloff.javaweb2015.webapp.page.EntityEditPage;

public class AirportListPanel extends Panel {

	@Inject
	private AirportService airportService;

	public AirportListPanel(String id) {
		super(id);

		AirportDataProvider airportDataProvider = new AirportDataProvider();

		WebMarkupContainer tableBody = new WebMarkupContainer("wrapper-body");
		add(tableBody);

		DataView<Airport> dv = new DataView<Airport>("list", airportDataProvider, 25) {

			IResource editIconResource = new ContextRelativeResource("img/editIcon.png");
			IResource deleteIconResource = new ContextRelativeResource("img/deleteIcon.png");

			@Override
			protected void populateItem(Item<Airport> item) {
				Airport airport = item.getModelObject();
				item.add(new Label("id"));
				item.add(new Label("name"));
				item.add(new Label("iataCode"));
				item.add(new Label("city"));
				item.add(new Label("taxes"));

				Link<Void> editLink = new Link<Void>("edit") {

					@Override
					public void onClick() {
						setResponsePage(new EntityEditPage(airport));
					}

				};

				editLink.add(new Image("editicon", editIconResource));
				item.add(editLink);

				Link<Void> deleteLink = new Link<Void>("delete") {

					@Override
					public void onClick() {
						// TODO Auto-generated method stub

					}

				};

				deleteLink.add(new Image("deleteicon", deleteIconResource));
				item.add(deleteLink);
			}

		};

		tableBody.add(dv);
		add(new PagingNavigator("paging", dv));

		add(new OrderByBorder<SingularAttribute<Airport, ?>>("sortByName", Airport_.name, airportDataProvider));
		add(new OrderByBorder<SingularAttribute<Airport, ?>>("sortByCity", Airport_.city, airportDataProvider));

		add(new Link<Void>("create") {

			@Override
			public void onClick() {
				setResponsePage(new EntityEditPage(new Airport()));

			}

		});
	}

	private class AirportDataProvider extends SortableDataProvider<Airport, SingularAttribute<Airport, ?>> {

		public AirportDataProvider() {
			super();
			setSort(Airport_.name, SortOrder.ASCENDING);
		}

		@Override
		public Iterator<? extends Airport> iterator(long first, long count) {
			SingularAttribute<Airport, ?> sortParam = getSort().getProperty();
			SortOrder propertySortOrder = getSortState().getPropertySortOrder(sortParam);
			boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);
			return airportService.getAll(sortParam, ascending, (int) first, (int) count).iterator();
		}

		@Override
		public long size() {
			return airportService.getCount();
		}

		@Override
		public IModel<Airport> model(Airport airport) {
			return new CompoundPropertyModel<Airport>(airport);
		}

	}

}
